package com.espirit.eap.manager;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.espirit.eap.pagelayout.ModelService;
import com.googlecode.cswish.struts.spring.Executor;

@Service
public class EspiritSystemLogRegularService {
	
	@Resource
	SystemLogService systemLogService;
	
	@Resource
	ModelService modelService;

	public void registerSystemLogRegular() {
		systemLogService.registerRegular(new Executor<Object, Object[]>() {
			@Override
			public SystemLog execute(Object[] parameters) {
				return getSystemLog((String)parameters[0], (String)parameters[1], (String)parameters[2]);
			}
		});
	}
	
	private SystemLog getSystemLog(String modelSimpleName, String operation, String fromMenu){
		if (StringUtils.isEmpty(modelSimpleName) || StringUtils.isEmpty(operation)) return null; 
		String[] modelPrefixs = new String[]{"espirit.", "hr."};
		boolean matched = false;
		for(String prefix : modelPrefixs){
			if (modelSimpleName.startsWith(prefix)) {
				matched = true;
				break;
			}
		}
		if (!matched) return null;
		
		String[] restrictModels = new String[]{"espirit.common.sysLog","espirit.eap.manager.systemLog"};
		
		for(String model : restrictModels){
			if (model.equals(modelSimpleName)) return null;
		}
		
		String[] restrictMethods = new String[]{"getLayout", "searchModelList"};
		for(String method : restrictMethods){
			if (method.equals(operation)) return null;
		}
		if (operation.equals("search") || operation.equals("searchAccount")){
			if (StringUtils.isEmpty(fromMenu) ){
				return null;
			}
		} else if (operation.startsWith("_")){
			return null;
		}
		String cnModelName = modelService.getI18nValue(modelSimpleName);
		String cnOperation = modelService.getButtonI18nValue(modelSimpleName, operation);
		if (StringUtils.isEmpty(cnOperation)){
			cnOperation = modelService.getButtonI18nValue(modelSimpleName, "_" +operation);
		}
		if (StringUtils.isEmpty(cnOperation)){
			if (operation.equals("update")){
				cnOperation = "修改";
			}
		}
		SystemLog systemLog = new SystemLog();
		systemLog.setModelName(cnModelName);
		systemLog.setOperation(cnOperation);
		if (StringUtils.isEmpty(cnModelName) || StringUtils.isEmpty(cnOperation)) return null;
		return systemLog;
	}
}
